It shows how to decode lossless compression stream from the camera into raw data via high-bandwidth lossless compression related algorithm.
10 currentsystem = platform.system()
11 if currentsystem ==
'Windows':
12 sys.path.append(os.path.join(os.getenv(
'MVCAM_COMMON_RUNENV'),
"Samples",
"Python",
"MvImport"))
14 sys.path.append(os.path.join(
"..",
"..",
"MvImport"))
15 from MvCameraControl_class
import *
18 if sys.version_info[0] < 3:
20 input_func = raw_input
28 Safely decode a string from a ctypes character array. 29 Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments. 31 byte_str = memoryview(ctypes_char_array).tobytes()
34 null_index = byte_str.find(b
'\x00')
36 byte_str = byte_str[:null_index]
39 for encoding
in [
'gbk',
'utf-8',
'latin-1']:
41 return byte_str.decode(encoding)
42 except UnicodeDecodeError:
46 return byte_str.decode(
'latin-1', errors=
'replace')
50 if __name__ ==
"__main__":
53 MvCamera.MV_CC_Initialize()
55 SDKVersion = MvCamera.MV_CC_GetSDKVersion()
56 print (
"SDKVersion[0x%x]" % SDKVersion)
58 deviceList = MV_CC_DEVICE_INFO_LIST()
59 tlayerType = (MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE
60 | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE)
63 ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
65 print (
"enum devices fail! ret[0x%x]" % ret)
68 if deviceList.nDeviceNum == 0:
69 print (
"find no device!")
72 print (
"Find %d devices!" % deviceList.nDeviceNum)
74 for i
in range(0, deviceList.nDeviceNum):
75 mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
76 if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE
or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
77 print (
"\ngige device: [%d]" % i)
78 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
79 print (
"device model name: %s" % strModeName)
80 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber)
81 print(
"device serial number: %s" % strSerialNumber)
82 nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
83 nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
84 nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
85 nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
86 print (
"current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
87 elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
88 print (
"\nu3v device: [%d]" % i)
89 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
90 print (
"device model name: %s" % strModeName)
92 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
93 print (
"device serial number: %s" % strSerialNumber)
94 elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE:
95 print (
"\nCML device: [%d]" % i)
96 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName)
97 print (
"device model name: %s" % strModeName)
99 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber)
100 print (
"device serial number: %s" % strSerialNumber)
101 elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE:
102 print (
"\nCXP device: [%d]" % i)
103 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName)
104 print (
"device model name: %s" % strModeName)
106 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber)
107 print (
"device serial number: %s" % strSerialNumber)
108 elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE:
109 print (
"\nXoF device: [%d]" % i)
110 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName)
111 print (
"device model name: %s" % strModeName)
113 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber)
114 print (
"device serial number: %s" % strSerialNumber)
116 nConnectionNum =
input_func(
"please input the number of the device to connect:")
118 if int(nConnectionNum) >= deviceList.nDeviceNum:
119 print (
"intput error!")
126 stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
128 ret = cam.MV_CC_CreateHandle(stDeviceList)
130 raise Exception (
"create handle fail! ret[0x%x]" % ret)
133 ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
135 raise Exception (
"open device fail! ret[0x%x]" % ret)
138 if stDeviceList.nTLayerType == MV_GIGE_DEVICE
or stDeviceList.nTLayerType == MV_GENTL_GIGE_DEVICE:
139 nPacketSize = cam.MV_CC_GetOptimalPacketSize()
140 if int(nPacketSize) > 0:
141 ret = cam.MV_CC_SetIntValue(
"GevSCPSPacketSize",nPacketSize)
143 print (
"Warning: Set Packet Size fail! ret[0x%x]" % ret)
145 print (
"Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
148 ret = cam.MV_CC_SetEnumValue(
"TriggerMode", MV_TRIGGER_MODE_OFF)
150 raise Exception (
"set trigger mode fail! ret[0x%x]" % ret)
153 nDevPayloadSize = c_uint64()
154 nMemAlignment = c_uint()
155 ret = cam.MV_CC_GetPayloadSize(nDevPayloadSize, nMemAlignment)
157 raise Exception (
"Get PayloadSize fail! ret[0x%x]" % ret)
158 nPayloadSize = nDevPayloadSize.value
161 ret = cam.MV_CC_StartGrabbing()
163 raise Exception (
"start grabbing fail! ret[0x%x]" % ret)
166 stOutFrame = MV_FRAME_OUT()
167 stDecodeParam = MV_CC_HB_DECODE_PARAM()
168 memset(byref(stOutFrame), 0, sizeof(stOutFrame))
169 for i
in range(0, nImageNum):
170 ret = cam.MV_CC_GetImageBuffer(stOutFrame, 1000)
171 if None != stOutFrame.pBufAddr
and 0 == ret:
172 print(
"get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (
173 stOutFrame.stFrameInfo.nWidth, stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nFrameNum))
175 stDecodeParam.pSrcBuf = stOutFrame.pBufAddr
176 stDecodeParam.nSrcLen = stOutFrame.stFrameInfo.nFrameLen
177 stDecodeParam.pDstBuf = (c_ubyte * nPayloadSize)()
178 stDecodeParam.nDstBufSize = nPayloadSize
179 ret = cam.MV_CC_HBDecode(stDecodeParam)
181 cam.MV_CC_FreeImageBuffer(stOutFrame)
182 raise Exception(
"HB Decode fail! ret[0x%x]" % ret)
184 print(
"Decode succeed!")
187 file_path =
"Image_w%d_h%d_fn%d.bmp" %(stDecodeParam.nWidth,stDecodeParam.nHeight, stOutFrame.stFrameInfo.nFrameNum)
188 c_file_path = file_path.encode(
'ascii')
189 stSaveParam = MV_SAVE_IMAGE_TO_FILE_PARAM_EX()
190 stSaveParam.enPixelType = stDecodeParam.enDstPixelType
191 stSaveParam.nWidth = stDecodeParam.nWidth
192 stSaveParam.nHeight = stDecodeParam.nHeight
193 stSaveParam.nDataLen = stDecodeParam.nDstBufLen
194 stSaveParam.pData = stDecodeParam.pDstBuf
195 stSaveParam.enImageType = MV_Image_Bmp
196 stSaveParam.pcImagePath = ctypes.create_string_buffer(c_file_path)
197 stSaveParam.iMethodValue = 1
198 stSaveParam.nQuality = 80
199 ret = cam.MV_CC_SaveImageToFileEx(stSaveParam)
201 cam.MV_CC_FreeImageBuffer(stOutFrame)
202 raise Exception(
"save image fail! ret[0x%x]" % ret)
204 cam.MV_CC_FreeImageBuffer(stOutFrame)
205 print(
"save image success")
207 print(
"no data[0x%x]" % ret)
209 print (
"press Enter key to stop grabbing.")
213 ret = cam.MV_CC_StopGrabbing()
215 raise Exception (
"stop grabbing fail! ret[0x%x]" % ret)
219 ret = cam.MV_CC_CloseDevice()
221 raise Exception (
"close deivce fail! ret[0x%x]" % ret)
224 cam.MV_CC_DestroyHandle()
225 except Exception
as e:
227 cam.MV_CC_CloseDevice()
228 cam.MV_CC_DestroyHandle()
231 MvCamera.MV_CC_Finalize()